package minishell.cmdlet.buildin;

import java.io.File;
import java.util.List;

import org.lex.io.IO;

import minishell.cmdlet.AbstractCmdlet;
import minishell.object.CanonicalPathFileObject;
import minishell.object.IObject;
import minishell.object.ListUtilities;

public class CdCmdlet extends AbstractCmdlet
{
	private static final String NAME = "cd";
	private static final String DESCRIPTION = "cd";
	private static final String USAGE = "cd";
	
	public String getName()
	{
		return NAME;
	}

	public String getDescription()
	{
		return DESCRIPTION;
	}

	public String getUsage()
	{
		return USAGE;
	}

	/* 
	 * ·�����������·��(�����env.getDirectory())�����Ǿ��·��
	 * Ӧ����ֳ����ǣ�Ȼ�󷵻���ȷ��·��
	 * ����,��Щ�ļ����а�հ��ַ��ʱ��·������˫��Ű�������
	 * 
	 */
	public List<IObject> excute( String path )
	{
		//û�в����൱��pwd
		if( 0 == path.length() )
			return pwd();
		/**********************************/
		// ���·���а�˫��ţ����˫����Ƿ�ƥ��
		if( '"' == path.charAt( 0 ) )
		{
			//˫��Ų�ƥ��
			if( '"' != path.charAt( path.length() - 1 ) )
				return this.pathSyntaxError( path );
			/***************************************/
			if( 1 == path.length() )
				return this.pathSyntaxError( path );
			/**************************************/
			//˫���ƥ�䣬ȥ��˫���
			path = path.substring( 1, path.length() - 1 );
		}
		//ȥ��˫��ź󣬼����ǿ�·��
		if( 0 == path.length() )
			return this.pathSyntaxError( path );
		/***************************************/
		// ����path��һ�������·��
		File file = new File( path );
		// path�Ǿ��·��
		if( true == file.isAbsolute() )
			return this.setDirectoryTo( file ); 
		/*************************************/
		// path�����·��
		File dir = new File( this.getEnvironment().getDirectory(), path );
		return this.setDirectoryTo( dir );
	}
	
	private List<IObject> setDirectoryTo( File file )
	{
		if( false == file.exists() )
			return this.pathNotExistError( file );
		if( false == file.isDirectory() )
			return this.fileNotDirectoryError( file );
		this.getEnvironment().setDirectory( file );
		return pwd();
	}
	
	private List<IObject> pwd()
	{
		return ListUtilities.asList( new CanonicalPathFileObject(
			getEnvironment().getDirectory() ) );
	}
	
	private List<IObject> fileNotDirectoryError( File file )
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append( getStandardErrorHead() );
		buffer.append( '\'' ) .append( IO.getCanonicalPath( file ) );
		buffer.append( "\' is not a directory" );
		return ListUtilities.stringList( buffer.toString() );
	}
	
	private List<IObject> pathNotExistError( File path )
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append( getStandardErrorHead() );
		buffer.append( "cann't find path '" ).append( path ).append( '\'' );
		return ListUtilities.stringList( buffer.toString() );
	}
	
	private List<IObject> pathSyntaxError( String path )
	{
		StringBuilder buffer = new StringBuilder();
		buffer.append( getStandardErrorHead() );
		buffer.append( "syntax error in path '" );
		buffer.append( path ).append( '\'' );
		return ListUtilities.stringList( buffer.toString() );
	}
}
